/*
 *  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialSupplierException;
import com.pitop.material.dao.MaterialSupplierDAO;
import com.pitop.material.entity.MaterialSupplierState;
import com.pitop.material.entity.MaterialSupplier;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("materialSupplierSV")
@Slf4j
public class MaterialSupplierSVImpl extends BaseSVImpl<MaterialSupplier, Long> implements MaterialSupplierSV {

    @Autowired
    private MaterialSupplierDAO materialSupplierDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return materialSupplierDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(MaterialSupplier entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象MaterialSupplier
     * @param id * @param suppierCode 供应商编码* @param materialCode 物料编码
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier load(java.lang.Long id,java.lang.String suppierCode,java.lang.String materialCode) {
           if(id==null&&suppierCode==null&&materialCode==null){
                 throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("suppierCode",suppierCode);
            param.put("materialCode",materialCode);
            return materialSupplierDAO.load(param);
    }
    /**
     * 加载一个对象MaterialSupplier 通过id
     * @param id 
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier loadById(java.lang.Long id) {
            if(id==null){
                 throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return materialSupplierDAO.loadById(id);
    }

    /**
     * 加载一个对象MaterialSupplier 通过suppierCode
     * @param suppierCode 供应商编码
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier loadBySuppierCode(java.lang.String suppierCode) {
            if(suppierCode==null){
                 throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return materialSupplierDAO.loadBySuppierCode(suppierCode);
    }

    /**
     * 加载一个对象MaterialSupplier 通过materialCode
     * @param materialCode 物料编码
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier loadByMaterialCode(java.lang.String materialCode) {
            if(materialCode==null){
                 throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return materialSupplierDAO.loadByMaterialCode(materialCode);
    }


    /**
    * 根据主键id,oldStates 共同更新 MaterialSupplier 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(java.lang.Long id,MaterialSupplierState newState,MaterialSupplierState... oldStates){
        if(id==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialSupplierDAO.updateStateById(id,new Date(),newState.name(), states);
    }
    /**
    * 根据主键suppierCode,oldStates 共同更新 MaterialSupplier 的状态到newState状态
    *
    * @param suppierCode 供应商编码
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateBySuppierCode(java.lang.String suppierCode,MaterialSupplierState newState,MaterialSupplierState... oldStates){
        if(suppierCode==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialSupplierDAO.updateStateBySuppierCode(suppierCode,new Date(),newState.name(), states);
    }
    /**
    * 根据主键materialCode,oldStates 共同更新 MaterialSupplier 的状态到newState状态
    *
    * @param materialCode 物料编码
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByMaterialCode(java.lang.String materialCode,MaterialSupplierState newState,MaterialSupplierState... oldStates){
        if(materialCode==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialSupplierDAO.updateStateByMaterialCode(materialCode,new Date(),newState.name(), states);
    }

    /**
    * 根据主键id 更新 MaterialSupplier 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(java.lang.Long id,MaterialSupplierState state){
            if(id==null){
               throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          materialSupplierDAO.updateById(id,state.name(),new Date());
    }
    /**
    * 根据主键suppierCode 更新 MaterialSupplier 的状态到另一个状态
    *
    * @param suppierCode 供应商编码
    * @param state 状态
    */
    @Override
    public void updateBySuppierCode(java.lang.String suppierCode,MaterialSupplierState state){
            if(suppierCode==null){
               throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          materialSupplierDAO.updateBySuppierCode(suppierCode,state.name(),new Date());
    }
    /**
    * 根据主键materialCode 更新 MaterialSupplier 的状态到另一个状态
    *
    * @param materialCode 物料编码
    * @param state 状态
    */
    @Override
    public void updateByMaterialCode(java.lang.String materialCode,MaterialSupplierState state){
            if(materialCode==null){
               throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          materialSupplierDAO.updateByMaterialCode(materialCode,state.name(),new Date());
    }


    /**
     * 删除对象MaterialSupplier
     * @param id * @param suppierCode 供应商编码* @param materialCode 物料编码
     * @return MaterialSupplier
     */
    @Override
    public void delete(java.lang.Long id,java.lang.String suppierCode,java.lang.String materialCode) {
            if(id==null&&suppierCode==null&&materialCode==null){
                throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("suppierCode",suppierCode);
            param.put("materialCode",materialCode);
            materialSupplierDAO.delete(param);
    }



    /**
    * 查询MaterialSupplier分页
    *
    * @param materialSupplier  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<MaterialSupplier>
    */
    @Override
    public List<MaterialSupplier> list(MaterialSupplier materialSupplier, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (materialSupplier != null) {
               map = JSON.parseObject(JSON.toJSONString(materialSupplier, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return materialSupplierDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(MaterialSupplier materialSupplier){
        Map<String, Object> map = null;
        if (materialSupplier != null) {
           map = JSON.parseObject(JSON.toJSONString(materialSupplier, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return materialSupplierDAO.count(map);
     }

    /**
     * 查询MaterialSupplier分页
     *
     * @param id  * @param suppierCode  供应商编码* @param materialCode  物料编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MaterialSupplier>
     */
    @Override
    public List<MaterialSupplier> list(java.lang.Long id,java.lang.String suppierCode,java.lang.String materialCode,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("suppierCode",suppierCode);
            param.put("materialCode",materialCode);
            return materialSupplierDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id,java.lang.String suppierCode,java.lang.String materialCode) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("suppierCode",suppierCode);
            param.put("materialCode",materialCode);
            return materialSupplierDAO.count(param);
    }

}
